package com.heima.behavior.service.impl;
import java.util.Date;

import com.heima.model.behavior.dtos.FollowBehaviorDto;
import com.heima.model.behavior.pojos.ApFollowBehavior;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadUtil.AppThreadLocalUtil;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class ApFollowBehaviorServiceImpl implements ApFollowBehaviorService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 关注用户
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(FollowBehaviorDto dto) {
        //1.非空判断
        if (dto.getArticleId() == null || dto.getAuthorId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.获取登录用户
        ApUser user = AppThreadLocalUtil.getUser();
        //3.判断用户是否存在，是否游客
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户不存在");
        }
        //4.游客无权操作
        if (user.getId() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        //5.设置用户关注key，作者的粉丝key
        String follow_key = BehaviorConstants.USER_FOLLOW_ + user.getId();
        String fans_key = BehaviorConstants.USER_FANS_ + dto.getAuthorId();
        //6.判断是否有小key
        Boolean followkeyFlag = stringRedisTemplate.opsForHash().hasKey(follow_key, dto.getAuthorId() + "");
        //7.判断是关注
        //7.1并且传参是关注状态
        if (followkeyFlag && dto.getOperation() == 0) {
            return ResponseResult.okResult("关注成功");
        }
        //7.2并且传参是取消关注
        if (followkeyFlag && dto.getOperation() == 1) {
            //7.2.1删除redis，mongodb
            stringRedisTemplate.opsForHash().delete(follow_key, dto.getAuthorId() + "");
            stringRedisTemplate.opsForHash().delete(fans_key, user.getId() + "");
            Query query = Query.query(Criteria.where("userId").is(user.getId()).and("followId").is(dto.getAuthorId()));
            mongoTemplate.remove(query, ApFollowBehavior.class);
            return ResponseResult.okResult("取消关注成功");
        }
        //8.判断不是关注,并且参数是关注，新增redis，mongodb
        if (!followkeyFlag && dto.getOperation() == 0) {
            stringRedisTemplate.opsForHash().put(follow_key, dto.getAuthorId() + "","1");
            stringRedisTemplate.opsForHash().put(fans_key, user.getId() + "","1");

            ApFollowBehavior apFollowBehavior = new ApFollowBehavior();
            apFollowBehavior.setId(ObjectId.get().toHexString());
            apFollowBehavior.setUserId(Long.valueOf(user.getId()));
            apFollowBehavior.setArticleId(dto.getArticleId());
            apFollowBehavior.setFollowId(dto.getAuthorId());
            apFollowBehavior.setCreatedTime(new Date());

            mongoTemplate.save(apFollowBehavior);

        }

        return ResponseResult.okResult("关注成功");
    }
}

